package org.adorsys.aderp.booking.web.ext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.adorsys.aderp.cash.domain.CashRegItem;

public class GenericData {
	private Map<String, List<CashRegItem>> data = new HashMap<String, List<CashRegItem>>();
	private Map<CashRegItem, String> cashRegItemIndex = new HashMap<CashRegItem, String>();
	/**
	 * technical object. used to cash concurent access on the class.
	 */
	private static Object synchroneObject_ = new Object();
	private static GenericData genericData ;
	
	public GenericData() {
		this.data= new HashMap<String, List<CashRegItem>>();
		this.cashRegItemIndex = new HashMap<CashRegItem, String>();
	}
	//return a single instance of this class
	public static GenericData getInstance(){
		if(genericData == null){
			synchronized (synchroneObject_) {
				if(genericData == null){
					genericData = new GenericData();
				}
			}
		}
		return genericData;
	}
	
	public  void addCashRegItem(String collectionKey,CashRegItem cashRegItem){
		List<CashRegItem> list = data.get(collectionKey);
		if(list == null) list = new ArrayList<CashRegItem>();
		list.add(cashRegItem);
		cashRegItemIndex.put(cashRegItem, collectionKey);
	}
	public void addCashRegItem(String collectionKey,List<CashRegItem> cashRegItems){
		for (CashRegItem cashRegItem : cashRegItems) {
			addCashRegItem(collectionKey, cashRegItem);
		}
	}
	public CashRegItem getCashRegItem(CashRegItem cashRegItem){
		String string = cashRegItemIndex.get(cashRegItem);
		List<CashRegItem> list = data.get(string);
		for (CashRegItem regItem : list) {
			if(regItem.equals(cashRegItem)){
				return cashRegItem;
			}
		}
		return null;
	}
	public List<CashRegItem> getData(String collectionKey){
		return data.get(collectionKey);
	}
}
